package com.zut.hospital.service.pharmacy;

import com.alibaba.fastjson.JSON;
import com.zut.hospital.controller.pharmacy.PurchaseController;
import com.zut.hospital.dao.pharmacy.MedicineMapper;
import com.zut.hospital.dao.pharmacy.PurchaseMapper;
import com.zut.hospital.dao.pharmacy.SupplierMapper;
import com.zut.hospital.dao.staff.UserMapper;
import com.zut.hospital.entity.pharmacy.*;
import com.zut.hospital.entity.staff.User;
import com.zut.hospital.entity.staff.UserExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: hos-test
 * @description: wu
 * @author: 杨曼珍
 * @create: 2019-05-28 15:31
 **/
@Service
public class PurchaseServiceImpl implements PurchaseService {
    private static final Logger log = LoggerFactory.getLogger(PurchaseServiceImpl.class);
    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    MedicineMapper medicineMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public String layuiJsonParse(List<Map<String, String>> list,int size) {
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "success");
        layuiMap.put("count", size);
        layuiMap.put("data", list);
        return JSON.toJSONString(layuiMap);
    }

    public Map selectAll(Purchase purchase){
        Map<String,String>map;

       MedicineExample medicineExample=new MedicineExample();
       medicineExample.createCriteria().andIdEqualTo(purchase.getPchMdcId());
        Medicine medicine=medicineMapper.selectByExample(medicineExample).get(0);
        String mdc_name=medicine.getMdcName();

        UserExample userExample=new UserExample();
        userExample.createCriteria().andIdEqualTo(purchase.getPchPhysicianId());
        User user=userMapper.selectByExample(userExample).get(0);
        String userName=user.getAdName();

        SupplierExample supplierExample=new SupplierExample();
        supplierExample.createCriteria().andIdEqualTo(purchase.getPchSupplierId());
        Supplier supplier=supplierMapper.selectByExample(supplierExample).get(0);
        String supplierName=supplier.getSupName();


        map=new HashMap<>();
        map.put("name",mdc_name);
        map.put("id",purchase.getId().toString());
        map.put("number",purchase.getPchNumber());
        map.put("count",purchase.getPchCount().toString());
        map.put("pchCount",purchase.getPchCount().toString());
        map.put("date",new SimpleDateFormat("yyyy-MM-dd").format(purchase.getPchDate()));
        map.put("price",purchase.getPchPrice().toString());
        map.put("suppiler",supplierName);
        map.put("physician",userName);
        return map;
    }

/*所有purchase*/
    @Override
    public  List<Purchase> getAll(){
        PurchaseExample purchaseExample=new PurchaseExample();
        List<Purchase>purchaseList= purchaseMapper.selectByExample(purchaseExample);
        return purchaseList;

    }
    /*分页*/

    @Override
    public String pagePurchase(int currPage,int pageSize){
        int size=getAll().size();
        PurchaseExample purchaseExample=new PurchaseExample();
        List<Purchase>purchaseList= purchaseMapper.selectByExample(purchaseExample);
        List<Map<String,String>>resaultmap=new ArrayList<>();
        int firstIndex=(currPage-1)*pageSize;
        int lastIndex=currPage*pageSize;
        if(currPage*pageSize>size){
             lastIndex=size;
        }
        for (Purchase purchase:purchaseList.subList(firstIndex,lastIndex)){
            resaultmap.add(selectAll(purchase));
        }
        return layuiJsonParse(resaultmap,purchaseList.size());
    }
    /*@Override
    public String delPurchase(String a){
       PurchaseExample purchaseExample=new PurchaseExample();
       purchaseExample.createCriteria().andPchNumberEqualTo(a);
       purchaseMapper.deleteByExample(purchaseExample);
       return layuiJsonParse(getAll(),getAll().size());
    }*/
    //excel上传
    @Override
    public int insertPurchase(String med_name, double price, int count, Date dateString,String remark,String supplier,String physician)throws ParseException {
        MedicineExample medicineExample=new MedicineExample();
       medicineExample.createCriteria().andMdcNameEqualTo(med_name);
        List<Medicine> medicines=medicineMapper.selectByExample(medicineExample);
        if(medicines.size()==0)
            return 0;
        Medicine medicine=medicines.get(0);
        int med_id=medicine.getId();
        double t_total=price*count;



        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        Date nowTime = new Date();
        System.out.println(nowTime);
        String number=new SimpleDateFormat("yyyyMMddHHmmss").format(nowTime);

        SupplierExample supplierExample=new SupplierExample();
        supplierExample.createCriteria().andSupNameEqualTo(supplier);
        List<Supplier> supplier1=supplierMapper.selectByExample(supplierExample);
        if(supplier1.size()==0)
            return 0;
        Supplier supplier2=supplier1.get(0);
        int sup_id=supplier2.getId();


        UserExample userExample=new UserExample();
        userExample.createCriteria().andAdNameEqualTo(physician);
        List<User> users=userMapper.selectByExample(userExample);
        if(users.size()==0)
            return 0;
        User user=users.get(0);
        int user_id=user.getId();

        Purchase purchase=new Purchase();
        purchase.setPchCount(count);
        purchase.setPchDate(dateString);
        purchase.setPchMdcId(med_id);
        purchase.setPchNumber(number);
        purchase.setPchPhysicianId(user_id);
        purchase.setPchPrice(price);
        purchase.setPchRemark(remark);
        purchase.setPchSupplierId(sup_id);
        purchase.setPchTotal(t_total);

        purchaseMapper.insertSelective(purchase);
        return 1;
    }
    @Override
    public int deletePurchase(String number){
    PurchaseExample purchaseExample=new PurchaseExample();
    purchaseExample.createCriteria().andPchNumberEqualTo(number);
    purchaseMapper.deleteByExample(purchaseExample);
    return 1;
    }

    @Override
    public int updatePurchase(int id,String field,String value){
        int count;
        double price;
        PurchaseExample purchaseExample=new PurchaseExample();
        purchaseExample.createCriteria().andIdEqualTo(id);
        Purchase purchase=purchaseMapper.selectByExample(purchaseExample).get(0);
        if (field.equals("count")){
            count=Integer.parseInt(value);
            purchase.setPchCount(count);
        }
        else {
            price=Double.parseDouble(value);
            purchase.setPchPrice(price);
        }
        purchaseMapper.updateByExample(purchase,purchaseExample);
        return 1;
    }

    @Override
    public String findPurchaseByMedName(int currPage,int pageSize,String med_name){
        List<Medicine>medicineList=medicineMapper.selectMoHoName(med_name);
        List<Map<String,String>>resaultmap=new ArrayList<>();
        int a=0;
        for (Medicine medicine:medicineList){
            System.out.println("medicine_id:"+medicine.getId());

            PurchaseExample purchaseExample=new PurchaseExample();
            purchaseExample.createCriteria().andPchMdcIdEqualTo(medicine.getId());
            List<Purchase>purchaseList=purchaseMapper.selectByExample(purchaseExample);
            int size=purchaseList.size();
            a=size;
            System.out.println(a);
            int firstIndex=(currPage-1)*pageSize;//每一页第一项
            int lastIndex=currPage*pageSize;//每一页最后一项
            if(currPage*pageSize>size){//最后一页
                lastIndex=size;
            }
            for (Purchase purchase:purchaseList.subList(firstIndex,lastIndex)){
                resaultmap.add(selectAll(purchase));
            }
        }
        System.out.println(resaultmap.toString());

        return layuiJsonParse(resaultmap,a);
    }


}
